package com.jyxd.web.service.assessment.basic;

import com.jyxd.web.dao.basic.NurseRecordFormDao;
import com.jyxd.web.dao.basic.NurseRecordFormOutPutDao;
import com.jyxd.web.data.basic.NurseRecordForm;
import com.jyxd.web.data.basic.NurseRecordFormOutPut;
import com.jyxd.web.data.dto.HttpResult;
import com.jyxd.web.data.dto.NurseRecordFormDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.jyxd.web.util.ExecuteUtil.*;
import static com.jyxd.web.util.MybatisPlusUtil.getParamList;
import static com.jyxd.web.util.MybatisPlusUtil.getParamOneOrderByAsc;
import static com.jyxd.web.util.StringUtils.isNumeric;
import static com.jyxd.web.util.UUIDUtil.getUUID;

@Service
public class NurseRecordFormOutPutService {

    @Autowired
    private NurseRecordFormOutPutDao nurseRecordFormOutPutDao;

    @Autowired
    private NurseRecordFormDao nurseRecordFormDao;

    @Autowired
    private NurseRecordFormService nurseRecordFormService;

    public HttpResult queryList(Map<String, Object> map) {
        errorIfEmpty(map, "参数不能为空");
        errorIfEmpty(map.get("rowId"), "护理数据行id不能为空");
        return new HttpResult(200, nurseRecordFormOutPutDao.selectList(getParamOneOrderByAsc("row_id", map.get("rowId"), "1", "create_time")), "数据查询成功");
    }

    public HttpResult saveOrUpdate(NurseRecordFormDTO data) {
        try {
            errorIfEmpty(data, "实体参数数据不能为空");
            errorIfEmpty(data.getRowId(), "护理单行id不能为空");
            errorIfEmpty(data.getPatientId(), "患者id不能为空");
            String rowId = data.getRowId();
            String patientId = data.getPatientId();
            Map<String, Object> map = new HashMap<>();
            map.put("row_id", rowId);
            map.put("patient_id", patientId);
            if (isEmpty(data.getList()))  // 如果为空则表示全部删除
                return updateByPatientIdAndRowId(patientId, rowId);
            List<NurseRecordFormOutPut> outPutList = nurseRecordFormOutPutDao.selectByMap(map);
            if (isEmpty(outPutList))
                return saveNewData(data);
            nurseRecordFormOutPutDao.delete(getParamList(map));
            saveNewData(data);
            return new HttpResult(200, "数据修改成功");
        } catch (Exception e) {
            return new HttpResult(500, e.getMessage());
        }
    }

    /**
     * 新增新的出量数据
     *
     * @param data
     * @return
     */
    private HttpResult saveNewData(NurseRecordFormDTO data) {
        if (isEmpty(data.getList()))
            return new HttpResult(200, "新数据不存在");
        data.getList().forEach(nurseRecordFormOutPut -> {
            errorIfFalse(isNumeric(nurseRecordFormOutPut.getOutPutValue()), "请输入数字");
            nurseRecordFormOutPut.setId(getUUID());
            nurseRecordFormOutPut.setCreateTime(new Date());
            nurseRecordFormOutPut.setPatientId(data.getPatientId());
            nurseRecordFormOutPut.setRowId(data.getRowId());
            nurseRecordFormOutPut.setStatus("1");
            nurseRecordFormOutPutDao.insert(nurseRecordFormOutPut);
        });
//        updateNurseRecordFormOther(data);
        return new HttpResult(200, "数据新增成功");
    }

    /**
     * 修改护理单中的其它出量与总出量
     *
     * @param data
     */
    private void updateNurseRecordFormOther(NurseRecordFormDTO data) {
        NurseRecordForm nurseRecordForm = nurseRecordFormDao.selectById(data.getRowId());
        if (isEmpty(nurseRecordForm))
            return;
        //nurseRecordForm.setOtherOutNum(getOtherOutNum(data));
        nurseRecordForm.setOutputAmount(String.valueOf(nurseRecordFormService.calculateOutputAmount(nurseRecordForm)));
        nurseRecordFormDao.update(nurseRecordForm);
    }

    /**
     * 获取其它值
     *
     * @param data
     * @return
     */
    private String getOtherOutNum(NurseRecordFormDTO data) {
        if (isEmpty(data))
            return null;
        if (isEmpty(data.getList()))
            return null;
        StringBuffer str = new StringBuffer();
        data.getList().forEach(nurseRecordFormOutPut -> {
            str.append(nurseRecordFormOutPut.getOutPutName()).append(":").append(nurseRecordFormOutPut.getOutPutValue()).append("ml").append(";");
        });
        return str.toString();
    }

    /**
     * 根据条件逻辑删除数据
     *
     * @param patientId
     * @param rowId
     */
    public HttpResult updateByPatientIdAndRowId(String patientId, String rowId) {
        Map<String, Object> map = new HashMap<>();
        map.put("patientId", patientId);
        map.put("rowId", rowId);
        nurseRecordFormOutPutDao.updateByPatientIdAndRowId(map);
        return new HttpResult(200, "数据已全部清空");
    }
}
